package com.sduept.nwld.dataserver.manager.rota;

import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.model.rota.HolidayInfo;
import com.sduept.nwld.dataserver.model.rota.RotaInfo;
import com.sduept.nwld.dataserver.model.rota.RotaLocation;
import com.sduept.nwld.dataserver.model.rota.RotaPerson;
import com.sduept.permission.manager.DepartManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RotaManager {	
	@Autowired
	private SuperDAO dao;	
	@Autowired
	private DepartManager dm;	
	
	/**
	 * 按日期获取当日所有员工去向
	 * @param date
	 * @return
	 */
	public List<RotaLocation> findRotaLocationByDate(Date date){
		
		return dao.findByCondition("holiday=?", new Object[] {date},RotaLocation.class);
	}	
	/**
	 * 按日期获取当日指定员工去向
	 * @param date
	 * @return
	 */
	public RotaLocation findRotaLocationByDate(Date date,String personName){
		 List<RotaLocation> list = dao.findByCondition("holiday=? and personName=?", new Object[] {date,personName},RotaLocation.class);
		 if(list.size() > 0) {
			 return list.get(0);
		 } else {
			 return null;
		 }
	}
	
	/**
	 * 按日期获取当日所有员工值班相关信息
	 * @param date
	 * @return
	 */
	public List<RotaInfo> findAllRotaInfoByDate(Date date){
		return dao.findByCondition("date=?",new Object[]{date}, RotaInfo.class);
	}
	
	/**
	 * 按年获取所有节假日值班信息集合
	 * @param year
	 * @return
	 */
	public List<RotaInfo> findAllRotaOfHolidayInfoByYear(int year){
		return dao.findByCondition("year=? and isHoliday=1 and manonduty=1",new Object[]{year}, RotaInfo.class);
	}
	/**
	 * 按年获取所有节假日非值班人员信息集合（关注人员位置信息）
	 * @param year
	 * @return
	 */
	public List<RotaInfo> findAllunRotaOfHolidayInfoByYear(int year){
		return dao.findByCondition("year=? and isHoliday=1 and manonduty=0", new Object[]{year},RotaInfo.class);
	}
	
	/**
	 * 获取某节假日当天非值班人员信息集合（关注人员位置信息）
	 * @param holidayDate
	 * @return
	 */
	public List<RotaInfo> findAllunRotaOfHolidayInfoByDate(Date holidayDate){
		return dao.findByCondition("date=? and isHoliday=1 and manonduty=0", new Object[]{holidayDate},RotaInfo.class);
	}
	
	
	/**
	 * 新增一条RotaInfo记录
	 * @param rotaInfo
	 * @return
	 */
	public RotaInfo saveRotaInfo(RotaInfo rotaInfo){
		return dao.create(rotaInfo, RotaInfo.class);
	}
	
	/**
	 * 编辑一条RotaInfo记录
	 * @param rotaInfo
	 * @return
	 */
	public RotaInfo updateRotaInfo(RotaInfo rotaInfo){
		return dao.update(rotaInfo, RotaInfo.class);
	}
	
	/**
	 * 编辑一条RotaInfo记录
	 * @param rotaInfo
	 * @return
	 */
	public RotaLocation updateRotaLocation(RotaLocation rotaInfo){
		return dao.createOrUpdate(rotaInfo, RotaLocation.class);
	}
	public RotaPerson updateRotaPerson(RotaPerson rotaInfo){
		return dao.createOrUpdate(rotaInfo, RotaPerson.class);
	}
	/**
	 * 编辑一条RotaInfo记录
	 * @param rotaInfo
	 * @return
	 */
	public RotaPerson getRotaPerson(Date rotadate){
		 List<RotaPerson> findByCondition = dao.findByCondition(RotaPerson.class, "rotaDate=?", new Object[] {rotadate});
		 if(findByCondition.size() > 0) {
			 return findByCondition.get(0);
		 } else {
			 return null;
		 }
	}
	
	/**
	 * 按月获取员工的值班信息，控件数据懒加载时可用
	 * @param month
	 * @return
	 */
	public List<RotaInfo> findAllRotaInfoByMonth(int month){
		return dao.findByCondition("month=?", new Object[]{month}, RotaInfo.class);
	}
	
	/**
	 * 按年获取员工的值班信息，控件数据非懒加载时可用
	 */
	public List<RotaInfo> findAllRotaInfoByYear(int year){
		return dao.findByCondition("year=?", new Object[]{year}, RotaInfo.class);
	}
	
	/**
	 * 获取起止时间段内所有值班人员信息
	 * @param start
	 * @param end
	 * @return
	 */
	public List<RotaInfo> findAllRotaInfosBetweenStartDateAndEndDate(Date start,Date end){
		return dao.findByCondition("date between ? and ? and  manonduty=1",new Object[]{start,end}, RotaInfo.class);
	}
	
	/**
	 * 获取起止时间段内所有节假日值班信息
	 * @param start
	 * @param end
	 * @return
	 */
	public List<RotaInfo> findAllHolidayRotaInfosBetweenStartDateAndEndDate(Date start,Date end){
		return dao.findByCondition("date between ? and ? and  holiday=1",new Object[]{start,end}, RotaInfo.class);		
	}
	
	/**
	 * 获取起止时间段内所有周末值班信息
	 * @param start
	 * @param end
	 * @return
	 */
	public List<RotaInfo> findAllWeekRotaInfosBetweenStartDateAndEndDate(Date start,Date end){
		return dao.findByCondition("date between ? and ? and  weekEnd=1",new Object[]{start,end}, RotaInfo.class);		
	}
	
	
	/**
	 * 根据年获取节假日
	 * @param year
	 * @return
	 */
	public List<HolidayInfo> findHolidaysByYear(int year){
		return dao.findByCondition("year=?", new Object[]{year}, "order by sortOrder", null, HolidayInfo.class);
	}
	/**
	 * 根据年获取节假值班信息
	 * @param year
	 * @return
	 */
	public List<RotaPerson> findRotaPersonByYear(int year){
		return dao.findByCondition("year=?", new Object[]{year}, RotaPerson.class);
	}
	/**
	 * 根据节假日名称获取节假日
	 * @param year,holidayName
	 * @return
	 */
	public List<HolidayInfo> findHolidaysByName(String holidayName,int year){
		return dao.findByCondition("year=? and holidayName=?", new Object[]{year,holidayName}, "order by holiday", null, HolidayInfo.class);
	}
	/**
	 * 根据年份获取去向
	 * @param year
	 * @return
	 */
	public List<RotaLocation> findLocationsByYear(int year){
		return dao.findByCondition("year=?", new Object[]{year}, "order by sortOrder", null, RotaLocation.class);
	}
	/**
	 * 根据年份获取去向
	 * @param year
	 * @return
	 */
	public List<RotaLocation> findLocationsByDates(List<HolidayInfo> hs){
		
		Object[] array = new Object[hs.size()];
		for (int i = 0; i < array.length; i++) {
			array[i] = hs.get(i).getHoliday();
		}
		return dao.findByConditionIN("holiday", array , RotaLocation.class, null);
	}

}
